package contos.usuario;

import contos.util.HibernateUtil;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.SessionFactory;

/**
 *
 * @author jandir
 */
public class UsuarioManagedBean {

    /** Creates a new instance of UsuarioManagedBean */
    public UsuarioManagedBean() {
    }

    public Collection<Usuario> getUsuarios() {
        Collection<Usuario> to_return = new ArrayList<Usuario>();

        SessionFactory sf = HibernateUtil.getSessionFactory();
        Session session = sf.openSession();
        try {
            session.beginTransaction();
            List<Object[]> result = session.createSQLQuery( "select id,nome from schema_testes.usuario " ).list();
            for ( Object[] objects : result ) {
                Usuario u = new Usuario();
                u.setId( Long.parseLong( objects[0].toString() ) );
                u.setNome( objects[1].toString() );
                to_return.add( u );
            }
        } catch ( Exception e ) {
            e.printStackTrace();
        } finally {
            session.flush();
            session.getTransaction().commit();
            session.close();
        }

        return to_return;
    }
}
